package com.yxzz.core.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.yxzz.core.domain.PPatient;
import com.yxzz.core.domain.TUser;
import com.yxzz.core.mapper.TUserMapper;
import com.yxzz.core.service.ITUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 客户信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-12
 */
@Service
public class TUserServiceImpl implements ITUserService
{
    @Autowired
    private TUserMapper tUserMapper;

    /**
     * 查询客户信息
     * 
     * @param userId 客户信息主键
     * @return 客户信息
     */
    @Override
    public TUser selectTUserByUserId(Long userId)
    {
        return tUserMapper.selectTUserByUserId(userId);
    }

    @Override
    public TUser selectTUserByOpenId(String openId) {
        TUser tUser = new TUser();
        tUser.setOpenId(openId);
        List<TUser> list = tUserMapper.selectTUserList(tUser);
        if (CollectionUtils.isEmpty(list)){
            return null;
        }
        return list.get(0);
    }

    /**
     * 查询客户信息列表
     * 
     * @param tUser 客户信息
     * @return 客户信息
     */
    @Override
    public List<TUser> selectTUserList(TUser tUser)
    {
        return tUserMapper.selectTUserList(tUser);
    }

    /**
     * 新增客户信息
     * 
     * @param tUser 客户信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTUser(TUser tUser)
    {
        tUser.setCreateTime(DateUtils.getNowDate());
        int rows = tUserMapper.insertTUser(tUser);
        insertPPatient(tUser);
        return rows;
    }

    /**
     * 修改客户信息
     * 
     * @param tUser 客户信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTUser(TUser tUser)
    {
        tUser.setUpdateTime(DateUtils.getNowDate());
        tUserMapper.deletePPatientById(tUser.getUserId());
        insertPPatient(tUser);
        return tUserMapper.updateTUser(tUser);
    }

    /**
     * 批量删除客户信息
     * 
     * @param userIds 需要删除的客户信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTUserByUserIds(Long[] userIds)
    {
        tUserMapper.deletePPatientByIds(userIds);
        return tUserMapper.deleteTUserByUserIds(userIds);
    }

    /**
     * 删除客户信息信息
     * 
     * @param userId 客户信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTUserByUserId(Long userId)
    {
        tUserMapper.deletePPatientById(userId);
        return tUserMapper.deleteTUserByUserId(userId);
    }

    /**
     * 新增病人信息信息
     * 
     * @param tUser 客户信息对象
     */
    public void insertPPatient(TUser tUser)
    {
        List<PPatient> pPatientList = tUser.getPPatientList();
        Long userId = tUser.getUserId();
        if (StringUtils.isNotNull(pPatientList))
        {
            List<PPatient> list = new ArrayList<PPatient>();
            for (PPatient pPatient : pPatientList)
            {
                pPatient.setId(userId);
                list.add(pPatient);
            }
            if (list.size() > 0)
            {
                tUserMapper.batchPPatient(list);
            }
        }
    }
}
